package com.itany.crawler.proxy;

import com.itany.crawler.UrlBaseHandle;
import com.itany.crawler.Urls;
import com.itany.utils.Log;
import org.apache.http.HttpHost;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class MyProxyManager extends UrlBaseHandle {
    static boolean proxyed = false;

    public static Map<HttpHost, Integer> proxys = new HashMap<>();
    static Integer minNum = 30;
    static Integer maxNum = 100;
    public static int index = 0; //当前使用ip
    // 链接失败 减 1 成功加 5 为0 删除
    static int deafult_weight = 20;

    static int step = 5;

    static {
        MyProxy.proxys = proxys;
        Proxy89IP.proxys = proxys;
//        System.out.println("MyProxyManager" + proxys);
    }

    public MyProxyManager(boolean proxyed) {
        super(proxyed);
    }

    @Override
    public String handle(Urls urls) throws Exception {
//        MyProxy.proxys = proxys;
//        Proxy89IP.proxys = proxys;
        try {
            Log.logErr("MyProxy 添加ProxyIP" + proxys.size() + proxys.hashCode());
            new MyProxy(proxyed).handle(null);
            Log.logErr("MyProxy 添加添加ProxyIP 成功" + proxys.size());
        } catch (Exception e) {
            Log.logErr("MyProxy 获取代理ip失败 " + e.getMessage());
        }

        try {
            Log.logErr("Proxy89IP 添加添加ProxyIP 成功" + proxys.size());
            new Proxy89IP(proxyed).handle(null);
            Log.logErr("Proxy89IP 添加添加ProxyIP" + proxys.size());
        } catch (Exception e) {
            Log.logErr(" :Proxy89IP 获取代理ip失败 " + e.getMessage());
        }
        return null;
    }

    // 是否是找代理时进行代理 防止循环调用调用 //正常为true
    static Boolean flag = true;

    public static HttpHost getHost() throws Exception {
        if (proxys.size() < minNum) {
            synchronized (flag) {
                if (flag && proxys.size() < maxNum) {
                    flag = false;
                    try {
                        Thread.sleep(10000);
                        //添加ProxyIP
                        new MyProxyManager(proxyed).handle(null);
                    } finally {
                        flag = true;
                    }
                }
            }
        }
        HttpHost[] httpHosts = proxys.keySet().toArray(new HttpHost[0]);
        // 2147483647 int 最大值 短时间运行无需考虑
        return httpHosts[index++ % httpHosts.length];
    }


    /**
     * 成功加step
     */
    public static void incProxy(HttpHost hh) {
        Integer integer = proxys.get(hh);
        if (integer != null) {
            integer += step;
            proxys.put(hh, integer);
        }
    }

    /**
     * 失败 -1
     */
    public static void decProxy(HttpHost hh) {
        Integer integer = proxys.get(hh);
        if (integer != null) {
            if (integer > 500) { // 防止权重累加至太多太多
                integer /= 2;
            } else {
                integer--;
            }
            if (integer <= 0) {
                proxys.remove(hh);
            } else {
                proxys.put(hh, integer);
            }
        }
    }

    static {
        proxys.put(new HttpHost("60.195.62.97", 80, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("59.55.161.255", 3256, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("115.223.7.31", 80, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("183.245.6.17", 8080, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("59.55.164.142", 3256, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("111.225.153.176", 3256, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("58.220.95.79", 10000, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("59.55.164.103", 3256, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("59.55.162.105", 3256, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("117.35.255.49", 3000, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("60.185.203.159", 3000, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("222.74.202.244", 8080, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("117.65.1.137", 3256, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("60.182.178.177", 8888, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("59.55.160.27", 3256, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("61.163.32.88", 3128, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("114.116.225.242", 8000, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("175.7.199.172", 3256, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("59.55.164.161", 3256, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("59.55.160.27", 3256, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("61.163.32.88", 3128, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("114.116.225.242", 8000, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("175.7.199.172", 3256, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("59.55.164.161", 3256, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("59.55.166.116", 3256, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("61.191.85.75", 8888, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("59.55.161.57", 3256, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("123.171.42.66", 3256, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("59.55.166.83", 3256, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("59.55.164.108", 3256, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("117.65.1.70", 3256, "HTTP"), deafult_weight);
        proxys.put(new HttpHost("125.73.209.227", 80, "HTTP"), deafult_weight);
    }

}
